package com.joysuch.wwyt.workflow.repository;

import com.joysuch.wwyt.workflow.entity.WorkFlowProcessCodeStrategy;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author ZhaoQiuWei
 * @Date 2021/5/14 17:16
 */
public interface WorkFlowProcessCodeStrategyDao extends JpaRepository<WorkFlowProcessCodeStrategy, Long> {

//    WorkFlowProcessCodeStrategy findByProcessId(Long processId);

//    @Query(value = "SELECT MAX(s.biz_type_code_number) FROM wwyt_workflow_process_code_strategy s INNER JOIN wwyt_workflow_process p ON s.process_id = p.id WHERE p.biz_type_name = ?1 AND s.delete_flag = '0' AND  p.delete_flag = '0'", nativeQuery = true)
//    Integer getMaxTypeCodeNumber(String bizTypeName);

//    @Transactional
//    @Modifying
//    @Query(value = "UPDATE wwyt_workflow_process_code_strategy SET biz_type_code_number =?1 WHERE id IN (SELECT a.id FROM (SELECT s.id FROM wwyt_workflow_process_code_strategy s INNER JOIN wwyt_workflow_process p ON s.process_id = p.id WHERE p.biz_type_name = ?2  AND s.delete_flag = '0' AND  p.delete_flag = '0') a )", nativeQuery = true)
//    void updateBizTypeNumberByBizTypeName(int seed, String bizTypeName);


    @Query(value = "SELECT * FROM wwyt_workflow_process_code_strategy  WHERE code_strategy = ?1 AND delete_flag = '0'", nativeQuery = true)
    List<WorkFlowProcessCodeStrategy> findByCodeStrategy(Integer index);

    @Transactional
    @Modifying
    @Query(value = "UPDATE wwyt_workflow_process_code_strategy SET biz_code_number = ?1 WHERE id IN ?2", nativeQuery = true)
    void updateBizNumberByIds(Integer seed, List<Long> collect);

//    @Query(value = "SELECT DISTINCT(p.biz_type_name) FROM wwyt_workflow_process p INNER JOIN  wwyt_workflow_process_code_strategy s " +
//            "ON  p.id = s.process_id WHERE s.id IN ?1 AND p.delete_flag = '0' AND s.delete_flag ='0'", nativeQuery = true)
//    List<String> findBizTypeNameByIds(List<Long> collect);

    int countByCodePrefix(String codePrefix);

    @Query(value = "SELECT * FROM wwyt_workflow_process  WHERE name LIKE %:name% AND delete_flag = '0'", nativeQuery = true)
    List<WorkFlowProcessCodeStrategy> findByNameLike(@Param("name") String name);

    int countByName(String name);
}
